package cn.itcast.util;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 把字符串转换成日期
 */
public class StringToDateConverter  implements Converter<String ,Date> {
    /**
     * String source 传入进来字符串
     * @param source
     * @return
     */
    @Override
    public Date convert(String source) {
        if (source==null){
            throw new RuntimeException("请传入数据");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            return sdf.parse(source);
        } catch (ParseException e) {
            throw  new RuntimeException("数据类型转换出现错误");
        }
    }
}
